package dk.ucn.abhweather;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class WeatherActivity extends Activity {

	private static final String TAG = "WEATHER";

	private EditText locationEditText;
	private Button ok_button;
	private TextView textViewWeather;
	private TextView textViewCity;
	private ImageView iconImageView;

	private String city_search_feed;
	private City city;

	private Weather weather;
	private String weather_feed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		city = new City();
		weather = new Weather();
		
		locationEditText = (EditText) findViewById(R.id.enter_your_location);
		ok_button = (Button) findViewById(R.id.ok_button);
		ok_button.setText("OK");
		textViewCity = (TextView) findViewById(R.id.city_tv);
		textViewWeather = (TextView) findViewById(R.id.temperature_TV);
		iconImageView = (ImageView) findViewById(R.id.weatherIcon);
		iconImageView.setVisibility(View.INVISIBLE);
	}

	public void onClick(View view) {
		// Get search location feed by getting string from
		// /res/values/string.xml
		// + text from EditText
		String myLocation = getString(R.string.search_location)
				+ locationEditText.getText().toString();
		city_search_feed = myLocation;

		Thread thread1 = new Thread(new Runnable() {
			public void run() {
				refreshCity();
			}
		});
		thread1.start();

		do {
		} while (city.getId() == 0);

		weather_feed = getString(R.string.weather_feed)
				+ String.valueOf(city.getId());

		// New thread to get a city
		Thread thread2 = new Thread(new Runnable() {
			public void run() {
				refreshWeather();
			}
		});
		thread2.start();

		do {
		} while (weather.getIconUrl() == null);

		initIcons();

		textViewCity.setText(city.getDescription());
		textViewWeather.setText(weather.toString());
		iconImageView.setImageResource(map.get(weather.getIconUrl()));
		iconImageView.setVisibility(View.VISIBLE);

		city.setId(0);
		weather.setIconUrl(null);
	}

	private Map<String, Integer> map;

	private void initIcons() {
		map = new HashMap<String, Integer>();
		map.put("cond000.gif", R.drawable.cond000);
		map.put("cond001.gif", R.drawable.cond001);
		map.put("cond002.gif", R.drawable.cond002);
		map.put("cond003.gif", R.drawable.cond003);
		map.put("cond004.gif", R.drawable.cond004);
		map.put("cond005.gif", R.drawable.cond005);
		map.put("cond006.gif", R.drawable.cond006);
		map.put("cond007.gif", R.drawable.cond007);
		map.put("cond008.gif", R.drawable.cond008);
		map.put("cond009.gif", R.drawable.cond009);
		map.put("cond010.gif", R.drawable.cond010);
		map.put("cond011.gif", R.drawable.cond011);
		map.put("cond012.gif", R.drawable.cond012);
		map.put("cond013.gif", R.drawable.cond013);
		map.put("cond014.gif", R.drawable.cond014);
		map.put("cond015.gif", R.drawable.cond015);
		map.put("cond016.gif", R.drawable.cond016);
		map.put("cond017.gif", R.drawable.cond017);
		map.put("cond018.gif", R.drawable.cond018);
		map.put("cond019.gif", R.drawable.cond019);
		map.put("cond020.gif", R.drawable.cond020);
		map.put("cond021.gif", R.drawable.cond021);
		map.put("cond022.gif", R.drawable.cond022);
		map.put("cond023.gif", R.drawable.cond023);
		map.put("cond024.gif", R.drawable.cond024);
		map.put("cond025.gif", R.drawable.cond025);
		map.put("cond026.gif", R.drawable.cond026);
		map.put("cond027.gif", R.drawable.cond027);
		map.put("cond028.gif", R.drawable.cond028);
		map.put("cond029.gif", R.drawable.cond029);
		map.put("cond030.gif", R.drawable.cond030);
		map.put("cond031.gif", R.drawable.cond031);
		map.put("cond032.gif", R.drawable.cond032);
		map.put("cond033.gif", R.drawable.cond033);
		map.put("cond034.gif", R.drawable.cond034);
		map.put("cond035.gif", R.drawable.cond035);
		map.put("cond036.gif", R.drawable.cond036);
		map.put("cond037.gif", R.drawable.cond037);
		map.put("cond038.gif", R.drawable.cond038);
		map.put("cond039.gif", R.drawable.cond039);
		map.put("cond040.gif", R.drawable.cond040);
		map.put("cond041.gif", R.drawable.cond041);
		map.put("cond042.gif", R.drawable.cond042);
		map.put("cond043.gif", R.drawable.cond043);
		map.put("cond044.gif", R.drawable.cond044);
		map.put("cond045.gif", R.drawable.cond045);
		map.put("cond046.gif", R.drawable.cond046);
		map.put("cond047.gif", R.drawable.cond047);
		map.put("cond048.gif", R.drawable.cond048);
		map.put("cond049.gif", R.drawable.cond049);
		map.put("cond050.gif", R.drawable.cond050);
		map.put("cond051.gif", R.drawable.cond051);
		map.put("cond052.gif", R.drawable.cond052);
		map.put("cond053.gif", R.drawable.cond053);
		map.put("cond054.gif", R.drawable.cond054);
		map.put("cond055.gif", R.drawable.cond055);
		map.put("cond056.gif", R.drawable.cond056);
		map.put("cond057.gif", R.drawable.cond057);
		map.put("cond058.gif", R.drawable.cond058);
		map.put("cond059.gif", R.drawable.cond059);
		map.put("cond060.gif", R.drawable.cond060);
		map.put("cond061.gif", R.drawable.cond061);
		map.put("cond062.gif", R.drawable.cond062);
		map.put("cond063.gif", R.drawable.cond063);
		map.put("cond064.gif", R.drawable.cond064);
		map.put("cond065.gif", R.drawable.cond065);
		map.put("cond066.gif", R.drawable.cond066);
		map.put("cond067.gif", R.drawable.cond067);
		map.put("cond068.gif", R.drawable.cond068);
		map.put("cond069.gif", R.drawable.cond069);
		map.put("cond070.gif", R.drawable.cond070);
		map.put("cond071.gif", R.drawable.cond071);
		map.put("cond072.gif", R.drawable.cond072);
		map.put("cond073.gif", R.drawable.cond073);
		map.put("cond074.gif", R.drawable.cond074);
		map.put("cond075.gif", R.drawable.cond075);
		map.put("cond076.gif", R.drawable.cond076);
		map.put("cond077.gif", R.drawable.cond077);
		map.put("cond078.gif", R.drawable.cond078);
		map.put("cond079.gif", R.drawable.cond079);
		map.put("cond080.gif", R.drawable.cond080);
		map.put("cond081.gif", R.drawable.cond081);
		map.put("cond082.gif", R.drawable.cond082);
		map.put("cond083.gif", R.drawable.cond083);
		map.put("cond084.gif", R.drawable.cond084);
		map.put("cond085.gif", R.drawable.cond085);
		map.put("cond086.gif", R.drawable.cond086);
		map.put("cond087.gif", R.drawable.cond087);
		map.put("cond088.gif", R.drawable.cond088);
		map.put("cond089.gif", R.drawable.cond089);
		map.put("cond090.gif", R.drawable.cond090);
		map.put("cond091.gif", R.drawable.cond091);
		map.put("cond092.gif", R.drawable.cond092);
		map.put("cond093.gif", R.drawable.cond093);
		map.put("cond094.gif", R.drawable.cond094);
		map.put("cond095.gif", R.drawable.cond095);
		map.put("cond096.gif", R.drawable.cond096);
		map.put("cond097.gif", R.drawable.cond097);
		map.put("cond098.gif", R.drawable.cond098);
		map.put("cond099.gif", R.drawable.cond099);
		map.put("cond100.gif", R.drawable.cond100);
		map.put("cond101.gif", R.drawable.cond101);
		map.put("cond102.gif", R.drawable.cond102);
		map.put("cond103.gif", R.drawable.cond103);
		map.put("cond104.gif", R.drawable.cond104);
		map.put("cond105.gif", R.drawable.cond105);
		map.put("cond106.gif", R.drawable.cond106);
		map.put("cond107.gif", R.drawable.cond107);
		map.put("cond108.gif", R.drawable.cond108);
		map.put("cond109.gif", R.drawable.cond109);
		map.put("cond110.gif", R.drawable.cond110);
		map.put("cond111.gif", R.drawable.cond111);
		map.put("cond112.gif", R.drawable.cond112);
		map.put("cond113.gif", R.drawable.cond113);
		map.put("cond114.gif", R.drawable.cond114);
		map.put("cond115.gif", R.drawable.cond115);
		map.put("cond116.gif", R.drawable.cond116);
		map.put("cond117.gif", R.drawable.cond117);
		map.put("cond118.gif", R.drawable.cond118);
		map.put("cond119.gif", R.drawable.cond119);
		map.put("cond120.gif", R.drawable.cond120);
		map.put("cond121.gif", R.drawable.cond121);
		map.put("cond122.gif", R.drawable.cond122);
		map.put("cond123.gif", R.drawable.cond123);
		map.put("cond124.gif", R.drawable.cond124);
		map.put("cond125.gif", R.drawable.cond125);
		map.put("cond126.gif", R.drawable.cond126);
		map.put("cond127.gif", R.drawable.cond127);
		map.put("cond128.gif", R.drawable.cond128);
		map.put("cond129.gif", R.drawable.cond129);
		map.put("cond130.gif", R.drawable.cond130);
		map.put("cond131.gif", R.drawable.cond131);
		map.put("cond132.gif", R.drawable.cond132);
		map.put("cond133.gif", R.drawable.cond133);
		map.put("cond134.gif", R.drawable.cond134);
		map.put("cond135.gif", R.drawable.cond135);
		map.put("cond136.gif", R.drawable.cond136);
		map.put("cond137.gif", R.drawable.cond137);
		map.put("cond138.gif", R.drawable.cond138);
		map.put("cond139.gif", R.drawable.cond139);
		map.put("cond140.gif", R.drawable.cond140);
		map.put("cond141.gif", R.drawable.cond141);
		map.put("cond142.gif", R.drawable.cond142);
		map.put("cond143.gif", R.drawable.cond143);
		map.put("cond144.gif", R.drawable.cond144);
		map.put("cond145.gif", R.drawable.cond145);
		map.put("cond146.gif", R.drawable.cond146);
		map.put("cond147.gif", R.drawable.cond147);
		map.put("cond148.gif", R.drawable.cond148);
		map.put("cond149.gif", R.drawable.cond149);
		map.put("cond150.gif", R.drawable.cond150);
		map.put("cond151.gif", R.drawable.cond151);
		map.put("cond152.gif", R.drawable.cond152);
		map.put("cond153.gif", R.drawable.cond153);
		map.put("cond154.gif", R.drawable.cond154);
		map.put("cond155.gif", R.drawable.cond155);
		map.put("cond156.gif", R.drawable.cond156);
		map.put("cond157.gif", R.drawable.cond157);
		map.put("cond158.gif", R.drawable.cond158);
		map.put("cond159.gif", R.drawable.cond159);
		map.put("cond160.gif", R.drawable.cond160);
		map.put("cond161.gif", R.drawable.cond161);
		map.put("cond162.gif", R.drawable.cond162);
		map.put("cond163.gif", R.drawable.cond163);
		map.put("cond164.gif", R.drawable.cond164);
		map.put("cond165.gif", R.drawable.cond165);
		map.put("cond166.gif", R.drawable.cond166);
		map.put("cond167.gif", R.drawable.cond167);
		map.put("cond168.gif", R.drawable.cond168);
		map.put("cond169.gif", R.drawable.cond169);
		map.put("cond170.gif", R.drawable.cond170);
		map.put("cond171.gif", R.drawable.cond171);
		map.put("cond172.gif", R.drawable.cond172);
		map.put("cond173.gif", R.drawable.cond173);
		map.put("cond174.gif", R.drawable.cond174);
		map.put("cond175.gif", R.drawable.cond175);
		map.put("cond176.gif", R.drawable.cond176);
		map.put("cond999.gif", R.drawable.cond999);
	}

	public void refreshCity() {
		// Get the XML
		URL url;

		try {
			url = new URL(city_search_feed);

			URLConnection connection;
			connection = url.openConnection();

			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();

			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpConnection.getInputStream();

				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();

				// Parse the location feed.
				Document dom = db.parse(in);
				Element docEle = dom.getDocumentElement();

				// Get a city entry.
				NodeList nl = docEle.getElementsByTagName("Location");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {
						Element locEle = (Element) nl.item(i);
						Element idEle = (Element) locEle.getElementsByTagName(
								"Id").item(0);
						Element descriptionEle = (Element) locEle
								.getElementsByTagName("Description").item(0);
						Element typeEle = (Element) locEle
								.getElementsByTagName("Type").item(0);
						int id = 0;
						if (idEle.getFirstChild().getNodeValue() != null) {
							id = Integer.parseInt(idEle.getFirstChild()
									.getNodeValue());
						} else {

						}
						String description = descriptionEle.getFirstChild()
								.getNodeValue();
						String type = typeEle.getFirstChild().getNodeValue();

						city.setId(id);
						city.setDescription(description);
						city.setType(type);
					}
				}
			}

		} catch (MalformedURLException e) {
			Log.d(TAG, "MalformedURLException");
		} catch (IOException e) {
			Log.d(TAG, "IOException");
		} catch (ParserConfigurationException e) {
			Log.d(TAG, "Parser Configuration Exception");
		} catch (SAXException e) {
			Log.d(TAG, "SAX Exception");
		} finally {
		}
	}

	public void refreshWeather() {
		// Get the XML
		URL url;

		try {
			url = new URL(weather_feed);

			URLConnection connection;
			connection = url.openConnection();

			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();

			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpConnection.getInputStream();

				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();

				// Parse the weather feed.
				Document dom = db.parse(in);
				Element docEle = dom.getDocumentElement();

				// Get a Description entry.
				NodeList nl = docEle.getElementsByTagName("Description");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {
						Element descriptionElement = (Element) nl.item(i);
						weather.setDescription(descriptionElement
								.getTextContent());
					}
				}

				nl = docEle.getElementsByTagName("Temperature");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {

						Element temperatureEle = (Element) nl.item(0);
						weather.setTemperature(Double
								.parseDouble(temperatureEle.getTextContent()));
					}
				}
				nl = docEle.getElementsByTagName("TemperatureUnit");
				if (nl != null && nl.getLength() > 0) {
					Element temperatureUnitEle = (Element) nl.item(0);
					weather.setTemperatureUnit(temperatureUnitEle
							.getTextContent());
				}
				nl = docEle.getElementsByTagName("WindDirection");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {

						Element windDirectionEle = (Element) nl.item(0);
						weather.setWindDirection(windDirectionEle
								.getTextContent());
					}
				}
				nl = docEle.getElementsByTagName("WindSpeed");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {

						Element windSpeedEle = (Element) nl.item(0);
						weather.setWindSpeed(Integer.parseInt(windSpeedEle
								.getTextContent()));
					}
				}
				nl = docEle.getElementsByTagName("WindSpeedUnit");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {

						Element windSpeedUnitEle = (Element) nl.item(0);
						weather.setWindSpeedUnit(windSpeedUnitEle
								.getTextContent());
					}
				}
				nl = docEle.getElementsByTagName("IconUrl");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {

						Element iconUrlEle = (Element) nl.item(0);
						weather.setIconUrl(iconUrlEle.getTextContent());
					}
				}
			}

		} catch (MalformedURLException e) {
			Log.d(TAG, "MalformedURLException");
		} catch (IOException e) {
			Log.d(TAG, "IOException");
		} catch (ParserConfigurationException e) {
			Log.d(TAG, "Parser Configuration Exception");
		} catch (SAXException e) {
			Log.d(TAG, "SAX Exception");
		} finally {
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.weather, menu);
		return true;
	}
}
